package com.probiz.estore.customer.service.impl;
 
import java.util.List;
import com.probiz.estore.common.model.customer.Feedback;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.FeedbackDao;
import com.probiz.estore.customer.model.FeedbackSearchCriteria;
import com.probiz.estore.customer.service.FeedbackManager;

/**
 * Feedback Business Delegate (Proxy) implementation to handle communication
 * between web and persistence layer. Implementation of FeedbackManager
 * interface. Developer introduced interfaces should be declared here. Won't get
 * overwritten.
 */
public class FeedbackManagerImpl extends GenericManagerImpl<Feedback> implements FeedbackManager {
	private FeedbackDao	feedbackDao;

	@Override
	protected void initManager() {
		this.dao = feedbackDao;
	}

	@Override
	protected void preDelete(Feedback entity) {
		
	}

	@Override
	protected void preSave(Feedback entity) {
		
	}

	public void setFeedbackDao(FeedbackDao feedbackDao) {
		this.feedbackDao = feedbackDao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#deleteFully(com.probiz.estore.common.model.customer.Feedback)
	 */
	public void deleteFully(Feedback feedback){
		//先删除反馈再统计
		this.delete(feedback);
		if(feedback.getParent() == null){
			//case:评论
		}else{
			//case:回复
			if(feedback.getStatus().equals(Feedback.STATUS_SHOW)){
				updateFeedbackReplyCount(feedback.getParent(),-1,-1);
			}else{
				updateFeedbackReplyCount(feedback.getParent(),-1,0);
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#searchByCriteria(com.probiz.estore.customer.model.FeedbackSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Feedback> searchByCriteria(FeedbackSearchCriteria feedbackSearchCriteria, SearchCriteria searchCriteria){
		return feedbackDao.searchByCriteria(feedbackSearchCriteria, searchCriteria);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#searchByCriteriaForController(com.probiz.estore.customer.model.FeedbackSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
    public List<Feedback> searchByCriteriaForController(FeedbackSearchCriteria feedbackSearchCriteria, SearchCriteria searchCriteria){
    	if (searchCriteria.getPageNo() < 1)  //如果是翻页过前，就拿第一页就可以。
        {
            searchCriteria.changePaging(1, searchCriteria.getPageSize());
        }
	    List<Feedback> result = searchByCriteria(feedbackSearchCriteria, searchCriteria);
	    if (result == null|| result.size() == 0 )
	    {
	        if (searchCriteria.getPageNo() > searchCriteria.getTotalPageCount()) //如果是翻页前，就拿最后一页就可以。
	        {
	            searchCriteria.changePaging(searchCriteria.getTotalPageCount(), searchCriteria.getPageSize());
	            result = searchByCriteria(feedbackSearchCriteria, searchCriteria);
	        }
	    }
		return result;
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.FeedbackManager#doMarkRead(com.probiz.estore.common.model.customer.Feedback)
     */
    public void doMarkRead(Feedback feedback){
    	feedback.setIsRead(Feedback.STATUS_READ);
		save(feedback);
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.FeedbackManager#doMarkUnRead(com.probiz.estore.common.model.customer.Feedback)
     */
    public void doMarkUnRead(Feedback feedback){
    	feedback.setIsRead(Feedback.STATUS_UNREAD);
		save(feedback);
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.FeedbackManager#doMarkShow(com.probiz.estore.common.model.customer.Feedback)
     */
    public void doMarkShow(Feedback feedback){
    	Short preStatus = feedback.getStatus().shortValue();
    	feedback.setStatus(Feedback.STATUS_SHOW);
    	save(feedback);
    	if(feedback.getParent() == null){
			//case:评论
    	}else{
			//case:回复
			if(!preStatus.equals(Feedback.STATUS_SHOW)){
				updateFeedbackReplyCount(feedback.getParent(),0,1);
			}
		}
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.FeedbackManager#doMarkUnShow(com.probiz.estore.common.model.customer.Feedback)
     */
    public void doMarkUnShow(Feedback feedback){
    	Short preStatus = feedback.getStatus().shortValue();
    	feedback.setStatus(Feedback.STATUS_UNSHOW);
    	save(feedback);
    	if(feedback.getParent() == null){
			//case:评论
		}else{
			//case:回复
			if(!preStatus.equals(Feedback.STATUS_UNSHOW)){
				updateFeedbackReplyCount(feedback.getParent(),0,-1);
			}
		}
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.FeedbackManager#saveReply(com.probiz.estore.common.model.customer.Feedback)
     */
	public void saveReply(Feedback reply){
		if(reply.getFeedbackId() == null){
			//默认情况： 新增反馈必定可见
			updateFeedbackReplyCount(reply.getParent(),1,1);
		}
		feedbackDao.save(reply);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#updateFeedbackReplyCount(com.probiz.estore.common.model.customer.Feedback, java.lang.Integer, java.lang.Integer)
	 */
	public void updateFeedbackReplyCount(Feedback feedback,Integer replyCount, Integer onShowReplyCount){
		if(feedback == null)return;
		feedback.setReplyCount(feedback.getReplyCount().intValue()+replyCount.intValue());
		feedback.setOnShowReplyCount(feedback.getOnShowReplyCount().intValue()+onShowReplyCount.intValue());
		this.save(feedback);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#doCalReplyCountForScheduling()
	 */
	public void doCalReplyCountForScheduling(){
		String hql = "from Feedback f where f.parent is null";//获得
		boolean flag =true;
		int pageSize = 10;
		int pageNo = 1;
		do{
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql,null,pageNo,pageSize,null);
			List<Feedback> feedbackList = searchByCriteria(sc);
			for(Feedback feedback: feedbackList){
				feedback.setReplyCount(feedbackDao.getReplyCountByFeedbackId(feedback.getFeedbackId()).intValue());
				feedback.setOnShowReplyCount(feedbackDao.getOnShowReplyCountByFeedbackId(feedback.getFeedbackId()).intValue());
				this.save(feedback);
			}
			if(feedbackList.size()!=pageSize){
				flag=false;
			}else{
				++pageNo;
			}
		}while(flag);	 
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#getOnShowReplyListByFeedbackId(java.lang.Integer)
	 */
	public List<Feedback> getOnShowReplyListByFeedbackId(Integer feedbackId){
		return feedbackDao.getOnShowReplyListByFeedbackId(feedbackId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FeedbackManager#getCountsOfCustomer(java.lang.Integer)
	 */
	public Integer[] getCountsOfCustomer(Integer customerId){
		return feedbackDao.getCountsOfCustomer(customerId);
	}
	
	/**
	 * 获得平台总反馈数量
	 * @return
	 */
	public Long countFeedbackTotal(){
		return feedbackDao.countFeedbackTotal();
	}
	/**
	 * 获取平台未回复的反馈数
	 * @return
	 */
	public Long countUnreplyFeedbackTotal(){
		return feedbackDao.countUnreplyFeedbackTotal();
	}
	
}